﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Silverlight.Testing;
using System8ToyMC;
using System8Equations;
using Numbers;
using System.Windows.Controls;
using ObjectDirectory;

namespace t_System8ToyMCControls
{
    [TestClass]
    public class t_System8ResultsUC : PresentationTest
    {
        System8ResultsUC _val;
        System8UpdatingResults _results;

        Value n, n1, n2, n12, p, p1, p2, p12;

        NamedValue _tb_efflt, _tb_effsmt, _tb_fakelt, _tb_fakesmt;


        [TestInitialize]
        public void TestSetup()
        {
            _val = new System8ResultsUC();

            n = new Value();
            n1 = new Value();
            n2 = new Value();
            n12 = new Value();
            p = new Value();
            p1 = new Value();
            p2 = new Value();
            p12 = new Value();

            _results = new System8UpdatingResults(n, p, n1, n2, p1, p2, n12, p12);
            _val.System8R = _results;

            TestPanel.Children.Add(_val);

            _tb_efflt = _val.FindName("efflt") as NamedValue;
            _tb_fakelt = _val.FindName("fakelt") as NamedValue;
            _tb_effsmt = _val.FindName("effsmt") as NamedValue;
            _tb_fakesmt = _val.FindName("fakesmt") as NamedValue;

        }

        [TestMethod]
        public void TestCTor()
        {
        }

        [TestMethod]
        public void TestFindAll()
        {
            Assert.IsNotNull(_tb_efflt, "eff lt missing");
            Assert.IsNotNull(_tb_effsmt, "eff smt missing");
            Assert.IsNotNull(_tb_fakelt, "fake lt missing");
            Assert.IsNotNull(_tb_fakesmt, "Fake smt missing");
        }

        [TestMethod]
        public void TestSetValues()
        {
            n.Val = 497008;
            p.Val = 26790;
            n1.Val = 60463;
            n2.Val = 213012;
            p1.Val = 7159;
            p2.Val = 16886;
            n12.Val = 36807;
            p12.Val = 5130;

            Assert.IsTrue(CloseTo(0.343996637890169, _tb_efflt.Value), "eff lt is not right");
            Assert.IsTrue(CloseTo(0.736690718499766, _tb_effsmt.Value), "eff smt is not right");
            Assert.IsTrue(CloseTo(0.050516373700457, _tb_fakelt.Value), "fake lt is not right");
            Assert.IsTrue(CloseTo(0.330012715126238, _tb_fakesmt.Value), "fake smt is not right");
        }

        public class UCContainer
        {
            Value n = new Value();
            Value p = new Value();
            Value n1 = new Value();
            Value p1 = new Value();
            Value n2 = new Value();
            Value p2 = new Value();
            Value n12 = new Value();
            Value p12 = new Value();

            public System8UpdatingResults uc { get; set; }
        };

        [TestMethod]
        public void TestPath()
        {
            UCContainer cont = new UCContainer() { uc = new System8UpdatingResults(n, p, n1, n2, p1, p2, n12, p12) };
            _val.Path = "uc";
            Assert.AreEqual("uc", _val.Path, "The raw path didn't come back right");

            ObjectPathManager mgr = new ObjectPathManager();

            Assert.AreEqual(mgr.GetPath(cont, cont.uc.fakeLTValue), _tb_fakelt.Path, "fake lt path incorrect");
            Assert.AreEqual(mgr.GetPath(cont, cont.uc.fakeSLTValue), _tb_fakesmt.Path, "fake lt path incorrect");
            Assert.AreEqual(mgr.GetPath(cont, cont.uc.effLTValue), _tb_efflt.Path, "fake lt path incorrect");
            Assert.AreEqual(mgr.GetPath(cont, cont.uc.effSLTValue), _tb_effsmt.Path, "fake lt path incorrect");

        }

        private bool CloseTo(double p1, double p2)
        {
            return (Math.Abs(p1 - p2) / p1 < 0.001);
        }
    }
}